package org.starsource.forum.client.view;

import java.util.Iterator;
import java.util.Set;

import org.starsource.forum.client.domain.CategoryObject;
import org.starsource.forum.client.handler.CategorySelectionHandler;
import org.starsource.forum.client.web.ForumService;
import org.starsource.forum.client.web.ForumServiceAsync;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.logical.shared.SelectionEvent;
import com.google.gwt.event.logical.shared.SelectionHandler;
import com.google.gwt.resources.client.ClientBundle;
import com.google.gwt.resources.client.ImageResource;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.AbstractImagePrototype;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Tree;
import com.google.gwt.user.client.ui.TreeItem;

/**
 * A tree displaying a set of email folders.
 */
public class Categories extends Composite {

	private final ForumServiceAsync forumService = GWT.create(ForumService.class);

	/**
	 * Specifies the images that will be bundled for this Composite and specify
	 * that tree's images should also be included in the same bundle.
	 */
	public interface Images extends ClientBundle, Tree.Resources {
		ImageResource drafts();

		ImageResource home();

		ImageResource inbox();

		ImageResource sent();

		ImageResource templates();

		ImageResource trash();

		@Source("noimage.png")
		ImageResource treeLeaf();
	}

	private final Images images = GWT.create(Images.class);

	private Tree tree = new Tree(images);

	private final TreeItem all = new TreeItem(
			imageItemHTML(images.home(), "所有栏目"));
	
//	private final TreeItem post = new TreeItem(
//			imageItemHTML(images.drafts(), "发表新帖"));
	
	private CategorySelectionHandler categorySelectionHandler = new CategorySelectionHandler() {

		@Override
		public void onCategorySelection(CategoryObject categoryObject) {
		}
			
	};
		
	public void setCategorySelectionHandler(CategorySelectionHandler categorySelectionHandler) {
		this.categorySelectionHandler = categorySelectionHandler; 
	}

	 /**
	 * Constructs a new mailboxes widget with a bundle of images.
	 * 
	 * @param images a bundle that provides the images for this widget
	 */
	public Categories() {
		tree.addItem(all);
//		tree.addItem(post);

		forumService.getCategorySet(new AsyncCallback<Set<CategoryObject>>() {

		@Override
		public void onFailure(Throwable caught) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void onSuccess(Set<CategoryObject> result) {
			Iterator<CategoryObject> iterator = result.iterator();
			while (iterator.hasNext()) {
				addImageItem(all, iterator.next());
			}
			all.setState(true);
		}
			
		});
		tree.addSelectionHandler(new SelectionHandler<TreeItem>() {

		@Override
		public void onSelection(SelectionEvent<TreeItem> event) {
			
			categorySelectionHandler.onCategorySelection((CategoryObject) event.getSelectedItem().getUserObject());
		}
			
		});

		initWidget(tree);
	}

	private TreeItem addImageItem(CategoryObject category) {
		ImageResource imageProto = (category.getParent() == null ? images.templates(): images.drafts());
			TreeItem item = new TreeItem(imageItemHTML(imageProto, category.getCaption()));
		item.setUserObject(category);
		Iterator<CategoryObject> iterator = category.getChildren().iterator();
		while (iterator.hasNext()) {
			addImageItem(item, iterator.next());
		}
		item.setState(true);
		return item;
		}
		private TreeItem addImageItem(TreeItem root, CategoryObject category) {
			TreeItem item = addImageItem(category);
			root.addItem(item);
			return item;
		}

	/**
	 * Generates HTML for a tree item with an attached icon.
	 * 
	 * @param imageProto the image prototype to use
	 * @param title the title of the item
	 * @return the resultant HTML
	 */
	private String imageItemHTML(ImageResource imageProto, String title) {
		return AbstractImagePrototype.create(imageProto).getHTML() + " " + title;
	}
}
